"""
Problem 41: https://projecteuler.net/problem=41

Pandigital prime

We shall say that an n-digit number is pandigital if it makes use of all the digits
1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/13                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
'''



import itertools
import p035
def isPandigitalPrime(m) -> bool:
    '''
    >>> assert isPandigitalPrime(2143)
    '''
    ms = str(m)
    if '0' in ms: return False
    d = len(ms)
    return set(ms) == set('123456789'[:d]) and p035.isPrime(int(m))


def maxPandigitalPrime_d(d: int) -> int:
    
    nums = sorted([''.join(item) for item 
                   in itertools.permutations('123456789'[:d])], reverse = True)

    for i in nums:
        if isPandigitalPrime(i):
            print(f'maxPandigitalPrime with {d}-digits: {i}')
            return int(i)

    print(f'PandigitalPrime with {d}-digits do not exist.')
    return 0
    
def solution(limit:int = 9) ->int:
    return max(maxPandigitalPrime_d(d) for d in range(2,limit+1))


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # 7652413

    '''
    PandigitalPrime with 2-digits do not exist.
    PandigitalPrime with 3-digits do not exist.
    maxPandigitalPrime with 4-digits: 4231
    PandigitalPrime with 5-digits do not exist.
    PandigitalPrime with 6-digits do not exist.
    maxPandigitalPrime with 7-digits: 7652413
    PandigitalPrime with 8-digits do not exist.
    PandigitalPrime with 9-digits do not exist.
    '''
